<?php
namespace Mobile\Controller;

use Mobile\Model\articleactionModel;
use Mobile\Model\articlecountModel;
use Mobile\Model\articleModel;
use Mobile\Model\articletypeModel;
use Mobile\Model\goodsModel;
use Mobile\Model\jubaoModel;
use Mobile\Model\rechargeModel;

class ArticleController extends BaseController
{
    public $articleactionModel;
    public $articlecountModel;
    public $articleModel;
    public $articletypeModel;
    public $atypes;
    public $suid;
    public $suidinfo;
    
    public function __construct() {
        parent::__construct();
        $this->articleactionModel = new articleactionModel();
        $this->articlecountModel = new articlecountModel();
        $this->articleModel = new articleModel();
        $this->articletypeModel = new articletypeModel();
        
        $this->_get_atype();//获取商品分类
        
        //如果有推荐人
         $suidinfo = array();
        
        $refer_uid = get('suid',$this->uid);
         
        if($refer_uid){
            $suidinfo = $this->getMemberInfobyUid($refer_uid);//获取信息
        }
        
         $this->suidinfo = $suidinfo;
        
        layout("Article/layout");
        //layout(false);
        
    }
    
    /*
     * 获取文章分类
     */
    public function _get_atype(){
        $data = $this->articletypeModel->getArticletypeList();
        $atypes = array();
        foreach($data as $k=>$v){
            $atypes[$v['atype_id']] = $v;
        }
        $this->atypes = $atypes;
        $this->assign('atypes',$atypes);
        return $atypes;
    }
    
    /*
     * 添加记录
     */
    protected function _addlog($article_id,$memberinfo,$suidinfo,$vtype){
        
        //文章总的数量加1
        if($vtype == 1){
            //阅读
            $article_data['view_nums'] = array("exp","view_nums+1");
        }else if($vtype == 2){
            //转发
            $article_data['zhuan_nums'] = array("exp","zhuan_nums+1");
        }if($vtype == 3){
            //赞
            $article_data['praise_nums'] = array("exp","praise_nums+1");
        }
        
        $where = array("article_id"=>$article_id);
        $res1 = $this->articleModel->editArticle($where, $article_data);
        
        if(!$res1){
            return false;
        }
        
        if(!$this->uid){
            return true;//操作成功,后面的不用了
        }
        
        //个人点赞记录
        $data = array(
            'uid' => $this->uid,
            "suid" => $suidinfo['uid'],
            'article_id' => $article_id,//文章id
            'vtype' => $vtype,//1阅读, 2转发, 3赞
            'create_time' => date("Y-m-d H:i:s"),//时间
            "atype" => 1,//文章
        );
        
        $res2 = $this->articleactionModel->addArticleaction($data);
        return $res2;
    }
    
    /*
     * 查找文章信息
     */
    protected function _getArticle($article_id){
        
        if(!$article_id){
            return false;
        }
        
        //判断文章是否存在
        $a_where = array(
            "article_id" => $article_id,
            "status" => 1,
        );
        
        $article = $this->articleModel->getArticle($a_where);
        if(empty($article)){
            return false;
        }
        
        return $article;
    }
   
    /*
     * 如果有转发人,给他发消息,告诉他有人阅读了他的文章
     */
    protected function _tellsuid($article,$memberinfo,$suidinfo,$vtype){
        //todo
        return true;
    }
    
    
    /*
     * 点赞
     */
    public function prise(){
        $article_id = get('article_id');
        $vtype = 3;
        
        $act = get('act');
        
        $article = $this->_getArticle($article_id);
        if(empty($article)){
            return_json(1,"当前文章已被删除");
        }

        //如果有推荐人
        if($this->suidinfo && $this->memberinfo){
            $this->_tellsuid($article,$this->memberinfo,$this->suidinfo,$vtype);//告诉推广人有人干嘛了
        }
        
        
        if($act == 'up'){
            //点赞
            $res = $this->_addlog($article_id,$this->memberinfo,$this->suidinfo,$vtype);
            if($res){
                return_json(0,"已点赞");
            }
            
        }else if($act == 'down'){
            
            if(!$this->uid){
                return_json(0,"已取消点赞");//没登陆,后面的不用了
            }
            
            //清除个人点赞记录
            $where1 = array(
                "uid" => $this->uid,
                "article_id" => $article_id,
                "vtype" => 3,
                "atype" => 1
            );
            $res = $this->articleactionModel->where($where1)->delete();
            //减少总的点赞记录
            
            //判断文章是否存在
            $a_where = array(
                "article_id" => $article_id,
                "status" => 1,
            );
            if($res){
                $res2 = $this->articleModel->where($a_where)->setDec("praise_nums");
                if($res2){
                    return_json(0,"已取消点赞");
                }
            }
            
        }

        return_json(1,"操作失败");
       
    }
    
    /*
     * 详情
     */
    public function index(){
        
        $article_id = get('article_id');
        $refresh = get('refresh');
        
        
        $article = $this->_getArticle($article_id);
        if(empty($article)){
            $this->error2("当前文章已被删除","/mobile/");
        }
        $article['content'] = stripslashes($article['content']);
        //拉取最新的用户信息
        if($refresh){
            $this->updateMemberSession($this->uid);
        }
        
        //判断是否是付费的
        if($article['is_free'] == 1){
            //无阅读权限
            $this->assign('can_view',true);
        }else{
            //付费内容
            
            //判断是否有权限阅读
            $grade_ids = $article['grade_ids'];
            if($grade_ids){
                //有等级控制
                $grade_ids = explode(",",$grade_ids);
                if(in_array($this->memberinfo['grade_id'],$grade_ids)){
                    //有阅读权限
                    $this->assign('can_view',true);
                }else{
                    //无阅读权限
                    $this->assign('can_view',false);
                }
            }else{
                //所有人都可以阅读
                //$this->assign('can_view',true);
            }
            
            if($this->memberinfo['grade_id']>1){
                $this->assign('can_view',true);
            } 
        }
        
        //关联商品
        //获取商品信息
        if($article['goods_id']){
            $goodsModel = new goodsModel();
            $where = array(
                "goods_id" => $article['goods_id'],
                "ischeck" => 1,
                "stock" => array("gt",0)
            );
            $goodsinfo = $goodsModel->getGoods($where);
            $this->assign('goods',$goodsinfo);
        }

        
        ///1阅读, 2转发, 3赞
        
        //判断当前文章我是否已经赞赏
        $prise_res = $this->_get_action($article_id,3);
        if($prise_res){
            $article['is_prise'] = 1;//已经点赞
        }else{
            $article['is_prise'] = 0;//没有点赞
        }
        
        //判断我以前是否看过
        $view_res = $this->_get_action($article_id,1);
        if($view_res){
            $article['is_view'] = 1;//已经看过
        }else{
            $article['is_view'] = 0;//第一次看
            //添加浏览记录
            $this->_addlog($article_id,$this->memberinfo,$this->suidinfo,1);
        }
        
        /*
         * 设置分享的图片
         */
        $headimgurl = $this->memberinfo['headimgurl'];//头像
        if(!$headimgurl){
            $headimgurl = $article['thumb']."!squareSmall";//文章缩略图
        }
        
        /*
         * 设置分享信息
         */
        $description = $article['description'] ? $article['description'] : C("SITE_NAME");
        $share_data = array(
            "title" => $article['title'],
            "description" => $description,
            "params" => "article_id=".$article_id,
            "img" => $headimgurl,
        );
        $this->setShare($share_data);
        
        /*
         * 设置标题
         */
        $seo_data = array(
            "title" => $article['title'],
            "keywords" => $article['keywords'],
            "description" => $article['description'],
        );
        $this->setSeo($seo_data);
        
        $this->assign("article",$article);
        

        
        //赞赏
        if($article['is_zanshang'] == 1){
            $rechargeModel = new rechargeModel();
            $where = array(
                "article_id" => $article_id,
                "checked" => 2
            );
            $dslist = $rechargeModel->getRechargeLists($where);
            $this->assign('dslist',$dslist);
            
            //判断我是否已经支付
            $where_me = array(
                "article_id" => $article_id,
                "checked" => 2,
                "uid" => $this->uid
            );
            $res = $rechargeModel->getRecharge($where_me);
            if($res){
                //已经支付
                $this->assign('is_pay',1);
            }else{
                $this->assign('is_pay',0);
            }
            
        }
        
        
        //获取讲师信息
        if($article['author_uid']){
            //$aduser = $this->getMemberInfobyUid($article['author_uid']);
            //当赏给病友
           // $this->assign('touser',$aduser);
        }else{
            //打赏给自己
            //$this->assign('touser',$this->memberinfo);
            
        }
        
        //如果有推荐人
        if($this->suidinfo){
            $this->_tellsuid($article,$this->memberinfo,$this->suidinfo,1);//告诉推广人有人干嘛了
            $this->assign('touser',$this->suidinfo);
        }else{
            //默认的推广员
            $default_uid = C("DEFAULT_SUID");
            $default_user = $this->getMemberInfobyUid($default_uid);
            $this->assign('touser',$default_user);
        }
        
        
        $this->display();
    }

    
    
    //文章列表
    public function lists()
    {

      $page = get("p",1);
      $atype_id = get('atype_id');
      
      //文章
      $where = array(
          "status" => 1,
          'uid' => 0,//管理员发布
          "post_time" => array("elt",date("Y-m-d H:i:s")),
          "is_check" => 1,
      );
      
      
      $title = get("title","");
      if(trim($title) != ""){
          $where["title"] = array("like","%{$title}%");
      }
      
      if($atype_id){
          $where["atype_id"] = $atype_id;
      }
      
      $article_list = $this->articleModel->getArticleList($where, $page);
      
      if(IS_AJAX){
          exit_json($article_list);
      }
      
      
      $this->assign('datalist',$article_list);
      
      $count = $this->articleModel->getArticleCount($where);
      $totalpage = ceil($count/C('PAGE_SIZE'));
      $this->assign('totalpage',$totalpage);

      /*
       * 设置分享信息
       */
      $share_data = array(
          "title" => C("SITE_NAME"),
      );
      $this->setShare($share_data);
      
     
      
      /*
       * 设置标题
      */
      $seo_data = array(
          "title" => C("SITE_NAME"),
          "keywords" => "",
          "description" => "",
      );
      $this->setSeo($seo_data);
      
      $this->display();
    }
    

    //添加转发
    public function addzhuanfa(){
        //添加转发数量
        $article_id = get('article_id');
        $res = $this->_addlog($article_id,$this->memberinfo,$this->suidinfo,2);
    }
    
    /*
     * 判断当前文章我是否已经
     * article_id 文章id
     * uid 用户id
     */
    protected function _get_action($article_id,$vtype){
        
        //判断有没登录
        if(!$this->uid){
            return false;
        }
        
        $where = array(
            "uid" => $this->uid,
            "article_id" => $article_id,
            "vtype" => $vtype,
            "atype" => 1
        );
        $res = $this->articleactionModel->getArticleaction($where);
        return $res;
    }
    
    /*
     * 举报
     */
    public function jubao(){
        layout(false);
        
        if(IS_AJAX){

            $data['complaintype'] = post('complaintype');
            $data['reasonText'] = post('reasonText');
            $data['uid'] = $this->uid;
            $data['article_id'] = $this->uid;
            $data['url'] = post('from_url');
            $data['article_id'] = post('article_id');
            
            $data['ip'] = get_client_ip();
            $data['uid'] = $this->uid;
            
            $data['create_time'] = date("Y-m-d H:i:s");
            $data['update_time'] = date("Y-m-d H:i:s");
            $data['status'] = 0;
            
            $data['nickname'] = $this->memberinfo['nickname'];
            $data['headimgurl'] = $this->memberinfo['headimgurl'];
            
            $jubaoModel = new jubaoModel();
            $res = $jubaoModel->addJubao($data);
            
            //当前文章对用户不可见
            $where = array("uid"=>$this->uid);
            if($this->uid){
              //  $this->memberModel->editMember($where, array("status"=>2));
            }
            
            if($res){
                return_json(0);
            }
            return_json(1);

        }
        
        
        $this->display();
    }


    /*
     * 朋友圈
     */
    public function shareline(){
        layout(false);
        
        /*
         * 设置分享信息
         */
        $share_data = array(
            "title" => C("SITE_NAME"),
        );
        $this->setShare($share_data);
        
        /*
         * 设置标题
        */
        $seo_data = array(
            "title" => C("SITE_NAME"),
            "keywords" => "",
            "description" => "",
        );
        $this->setSeo($seo_data);
        
        $this->display();
    }

    /*
     * 下载图片
     */
    public function downloadpic(){
        $dtype = get('dtype');
        $media_id = post("serverId");
        //从公众平台拉取最新的资料
        if(!$this->is_wechat){
            $this->wechat = new WechatController();
        }
        
        if($dtype == "shareline"){
            //朋友圈图片
            $data = $this->wechat->getMedia($media_id);
            if($data){
                //将图片上传到又拍云
                $res = $this->cuploud("shareline",$data);
                if($res){
                    //更新数据
                    $where = array(
                        'uid' => $this->uid
                    );
                    $pdata = array(
                        "shareline_pic" => $res
                    );
                    $this->memberModel->editMember($where, $pdata);
                }
                
            }
        }
        
        
        return_json(1);
    }
    
    /*
     * 二维码
     */
    public function qrcodeapi(){
        layout(false);
        $text = get('url');
        if($text){
            qrcode($text, false, $level = 1, $size = 8, $margin = 1,$saveandprint=false);
        }
    }
    

    
}